﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Veralkohol.BAMTService;
using Veralkohol.ViewModel;

namespace Veralkohol.Commons
{
    public class RemotePersistence : IDiagramPersistence
    {
        public void Save(PersistentDiagram data)
        {
            LikeAlcoholClient proxy = new LikeAlcoholClient();

            List<OneDrink> odrinks = new List<OneDrink>();

            try
            {
                foreach (var item2 in data.Drunked)
                {
                    foreach (var item in item2.drinks)
                    {

                        odrinks.Add(new OneDrink()
                        {
                            Amount = (int)item.quantity,
                            Beverage = new Beverage()
                            {
                                AlcoholPercent = (int)item.percentage,
                                Name = item.drinkname + "@" + item.imagename
                            },
                            Time = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day
                                ,item2.hour,item2.minute,0).AddDays(item2.day)
                        });
                    }
                }

                var guid = proxy.SaveOccasion(new Occasion()
                {
                    OccasionID = new Guid(),
                    DateTime = DateTime.Now,
                    Name = DateTime.Now.ToString(),
                    UserID = new Guid(),
                    DrinkList = odrinks.ToArray()
                });

                StreamWriter file = File.AppendText("server.saves");
                file.WriteLine("{0}%{1}", DateTime.Now, guid);
                file.Close();

                string messageBoxText = "A mentés sikerült! :)                              ";
                string caption = "Mentés :)";
                MessageBoxButton button = MessageBoxButton.OK;
                MessageBoxImage icon = MessageBoxImage.Information;
                MessageBox.Show(messageBoxText, caption, button, icon);
            }
            catch
            {
                string messageBoxText = "A mentés nem sikerült! :(                          ";
                string caption = "Mentés :(";
                MessageBoxButton button = MessageBoxButton.OK;
                MessageBoxImage icon = MessageBoxImage.Error;
                MessageBox.Show(messageBoxText, caption, button, icon);

                StreamWriter file = File.AppendText("server.saves");
                file.WriteLine("{0}%{1}", DateTime.Now, new Guid());
                file.Close();
            }
        }

        public PersistentDiagram Load()
        {
            LikeAlcoholClient proxy = new LikeAlcoholClient();


            try
            {
                var occ = proxy.GetOccasion(OpenViewModel.RemoteGuid);
                var t = occ.DrinkList.First().Time;
                var t10 = occ.DrinkList.First().Time.AddHours(10);

                return new PersistentDiagram()
                {
                    weight = 60,
                    xfrom = string.Format("{0}.{1}:{2}",0,t.Hour,t.Minute),
                    xto = string.Format("{0}.{1}:{2}", t10.Day - t.Day, t10.Hour, t10.Minute),
                    yfrom = 0,
                    yto = 6,
                    Drunked = occ.DrinkList.Select((dl) => new PersistentAlcoholicData()
                    {
                        day = t10.Day - t.Day,
                        hour = dl.Time.Hour,
                        minute = dl.Time.Minute,
                        drinks = new List<PersistentDrink>(new[] {new PersistentDrink()
                        {
                            percentage = dl.Beverage.AlcoholPercent, 
                            drinkname = dl.Beverage.Name.Split(new []{'@'})[0],
                            imagename = dl.Beverage.Name.Split(new []{'@'})[1],
                            quantity = dl.Amount
                        }})
                    }).ToList()
                };
            }
            catch
            {
                return null;
            }


        }
    }
}
